<template>
  <div class="app-container">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
      label-width="90px">
      <el-row>

        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="queryParams.mobile" placeholder="请输入手机号码" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item label="状态" prop="state">
          <el-select v-model="queryParams.state" placeholder="请选择状态" clearable size="small">
            <el-option v-for="dict in stateOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item style="margin-left: 22px;">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>

      </el-row>
      <el-row>
        <el-form-item label="发送时间" prop="sendDate">
          <el-date-picker v-model="queryParams.sendDate" type="daterange" align="right" value-format="yyyy-MM-dd"
            unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
            :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="row-mbinfo">

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div class="divTable">
      <el-table v-loading="loading" :data="smsList">
        <el-table-column label="手机号码" align="center" prop="mobile" :show-overflow-tooltip="true" />
        <el-table-column label="发送内容" align="center" prop="smsCode" :show-overflow-tooltip="true" />
        <el-table-column label="短信发送时间" align="center" prop="sendDate" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.sendDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="state" :formatter="stateFormat" />
        <el-table-column label="消息" align="center" prop="msg" :show-overflow-tooltip="true" />
        <el-table-column label="ip地址" align="center" prop="ip" :show-overflow-tooltip="true" />

      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改短信发送记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="平台ID" prop="platformId">
          <el-input v-model="form.platformId" placeholder="请输入平台ID" />
        </el-form-item>
        <el-form-item label="短信接收手机号码" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入短信接收手机号码" />
        </el-form-item>
        <el-form-item label="短信验证码" prop="smsCode">
          <el-input v-model="form.smsCode" placeholder="请输入短信验证码" />
        </el-form-item>
        <el-form-item label="短信发送时间" prop="sendDate">
          <el-date-picker clearable size="small" v-model="form.sendDate" type="date" value-format="yyyy-MM-dd"
            placeholder="选择短信发送时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="短信验证时间" prop="validateDate">
          <el-date-picker clearable size="small" v-model="form.validateDate" type="date" value-format="yyyy-MM-dd"
            placeholder="选择短信验证时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="验证内容，可能出现输入不对，重复验证" prop="validateCode">
          <el-input v-model="form.validateCode" placeholder="请输入验证内容，可能出现输入不对，重复验证" />
        </el-form-item>
        <el-form-item label="状态，0未验证  1已验证" prop="state">
          <el-select v-model="form.state" placeholder="请选择状态，0未验证  1已验证">
            <el-option v-for="dict in stateOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="有效时间" prop="expireDate">
          <el-date-picker clearable size="small" v-model="form.expireDate" type="date" value-format="yyyy-MM-dd"
            placeholder="选择有效时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="消息" prop="msg">
          <el-input v-model="form.msg" placeholder="请输入消息" />
        </el-form-item>
        <el-form-item label="ip地址" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入ip地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看短信发送记录对话框 -->
    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="700px" append-to-body>
      <el-form ref="detailForm" label-width="100px">
        <el-row>
          <el-col :span="12" v-for="(item, index) in showData" :key="index" style="margin-top:-15px">
            <el-form-item :label="`${item.label}：`">
              <span>{{item.value}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="handCloseDetail">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listSms,
    getSms,
    delSms,
    addSms,
    updateSms
  } from "@/api/platform/sms";

  export default {
    name: "Sms",
    components: {},
    data() {
      return {
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近六个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 180)
              picker.$emit('pick', [start, end])
            }
          }]
        },
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 短信发送记录表格数据
        smsList: [],
        // 弹出层标题
        title: "",
        //查看详细标题
        viewTitle: "",
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        // 状态，0未验证  1已验证字典
        stateOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          platformId: null,
          mobile: null,
          smsCode: null,
          sendDate: null,
          validateDate: null,
          validateCode: null,
          state: null,
          expireDate: null,
          msg: null,
          ip: null
        },
        columns: [{
            label: 'ip地址',
            dataIndex: 'sendId',
          },
          {
            label: '平台ID',
            dataIndex: 'platformId',
          },
          {
            label: '短信接收手机号码',
            dataIndex: 'mobile',
          },
          {
            label: '短信验证码',
            dataIndex: 'smsCode',
          },
          {
            label: '短信发送时间',
            dataIndex: 'sendDate',
          },
          {
            label: '短信验证时间',
            dataIndex: 'validateDate',
          },
          {
            label: '验证内容，可能出现输入不对，重复验证',
            dataIndex: 'validateCode',
          },
          {
            label: '状态，0未验证  1已验证',
            dataIndex: 'state',
          },
          {
            label: '有效时间',
            dataIndex: 'expireDate',
          },
          {
            label: '消息',
            dataIndex: 'msg',
          },
          {
            label: 'ip地址',
            dataIndex: 'ip',
          },
        ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getDicts("platform_send_sms_status").then(response => {
        this.stateOptions = response.data;
      });
    },
    methods: {
      /** 查询短信发送记录列表 */
      getList() {
        this.loading = true;
        if (this.queryParams.sendDate != null && this.queryParams.sendDate.length > 0) {
          let arr = this.queryParams.sendDate;
          this.queryParams.validateDate = arr[0] + " 00:00:00";
          this.queryParams.expireDate = arr[1] + " 23:59:59";
        }
        listSms(this.queryParams).then(response => {
          this.smsList = response.rows;
          this.total = response.total;
          this.loading = false;
          this.queryParams.validateDate = null;
          this.queryParams.expireDate = null;
        });
      },
      // 状态，0未验证  1已验证字典翻译
      stateFormat(row, column) {
        return this.selectDictLabel(this.stateOptions, row.state);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          sendId: null,
          platformId: null,
          mobile: null,
          smsCode: null,
          sendDate: null,
          validateDate: null,
          validateCode: null,
          state: null,
          expireDate: null,
          msg: null,
          ip: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.sendId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加短信发送记录";
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.viewOpen = true;
        this.viewTitle = "查看短信发送记录";
        this.showData = [];
        this.columns.forEach(item => {
          this.showData.push({
            label: item.label,
            value: row[item.dataIndex]
          })
        })
      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const sendId = row.sendId || this.ids
        getSms(sendId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改短信发送记录";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.sendId != null) {
              updateSms(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addSms(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const sendIds = row.sendId || this.ids;
        this.$confirm('是否确认删除短信发送记录编号为"' + sendIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSms(sendIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('platform/sms/export', {
          ...this.queryParams
        }, `platform_sms.xlsx`)
      }
    }
  };
</script>
